import Vue from 'vue'
import store from '@/store/'
import {
	isEmpty,
	addBaseURL
} from '@/utils/common.js'
// let baseURL = 'http://192.168.1.103:8081'
// let baseURL = 'http://123.57.232.1:8080'
// let baseURL = 'http://123.57.232.1:8091'
let baseURL = 'http://47.57.79.152:8091'
Vue.prototype.$baseURL = baseURL

export const requestG = (url, params, needToken = true) => {
	return new Promise((resolve, reject) => {
		uni.request({
			method: 'GET',
			url: baseURL + addBaseURL(url),
			data: params,
			header: needToken ? {
				'Authorization': store.getters.token
			} : {},
			success: res => resolve(res.data),
			fail: res => reject(res.data)
		});
	})
};

export const requestK = (url, params, needToken = true) => {
	return new Promise((resolve, reject) => {
		uni.request({
			method: 'POST',
			url: baseURL + addBaseURL(url),
			data: params,
			header: needToken ? {
				'Authorization': store.getters.token,
				'Content-Type': 'application/x-www-form-urlencoded'
			} : {
				'Content-Type': 'application/x-www-form-urlencoded'
			},
			success: res => resolve(res.data),
			fail: res => reject(res.data)
		});
	})
};

export const requestJ = (url, params, needToken = true) => {
	return new Promise((resolve, reject) => {
		uni.request({
			method: 'POST',
			url: baseURL + addBaseURL(url),
			data: params,
			header: needToken ? {
				'Authorization': store.getters.token
			} : {},
			success: res => resolve(res.data),
			fail: res => reject(res.data)
		});
	})
};

export const requestD = (url, params, needToken = true) => {
	return new Promise((resolve, reject) => {
		uni.request({
			method: 'DELETE',
			url: baseURL + addBaseURL(url),
			data: params,
			header: needToken ? {
				'Authorization': store.getters.token,
				'Content-Type': 'application/x-www-form-urlencoded'
			} : {
				'Content-Type': 'application/x-www-form-urlencoded'
			},
			success: res => resolve(res.data),
			fail: res => reject(res.data)
		});
	})
};

export const requestP = (url, params, needToken = true) => {
	return new Promise((resolve, reject) => {
		uni.request({
			method: 'PUT',
			url: baseURL + addBaseURL(url),
			data: params,
			header: needToken ? {
				'Authorization': store.getters.token,
				'Content-Type': 'application/x-www-form-urlencoded'
			} : {
				'Content-Type': 'application/x-www-form-urlencoded'
			},
			success: res => resolve(res.data),
			fail: res => reject(res.data)
		});
	})
};
